package com.woniu.humanresource.controller;

import com.woniu.humanresource.dao.AchievementDao;
import com.woniu.humanresource.dao.DepartmentsDao;
import com.woniu.humanresource.dao.UserDao;
import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.service.LoginService;
import com.woniu.humanresource.service.UserService;
import com.woniu.humanresource.service.impl.AchievementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
public class UserController {
    //自动注入登录业务层
    @Autowired
    UserService userService;

    @Autowired
    UserDao userDao;

    @Autowired
    DepartmentsDao departmentsDao;


    @PreAuthorize("hasAnyAuthority('manage')")
    @PostMapping("/users/{pageIndex}")
    public PageBean<User> users(
            @PathVariable("pageIndex") int pageIndex,
            HttpServletResponse response, HttpServletRequest request) {
        //查询所有管理员
        List<User> users = userService.selectAllUser(pageIndex, 5);
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setBeanList(users);
        //查询所有部门
        List<Departments> departments = departmentsDao.selectAllDepartment();
        userPageBean.setDepartments(departments);
        userPageBean.setTotalRecord(userService.selectAllUserCount(0, 0));
        return userPageBean;
    }


//    @PostMapping("/usersbycondition/{dapartid}")
//    public PageBean<User> usersbycondition(
//            @PathVariable("dapartid")  int dapartid,
//            HttpServletResponse response, HttpServletRequest request) {
//        //条件查询管理员
//        List<User> users = userService.selectUserByCondition(dapartid);
//        PageBean<User> userPageBean = new PageBean<>();
//        userPageBean.setBeanList(users);
//        //查询所有部门
//        List<Departments> departments = departmentsDao.selectAllDepartment();
//        userPageBean.setDepartments(departments);
//        //查询所有的职位
//        List<Position> positions = userService.selectAllPosition();
//        userPageBean.setPositions(positions);
//        return userPageBean;
//    }


    @PostMapping("/selectUserByDepartmentAndPosition/{dapartid}/{positionid}")
    public PageBean<User> selectUserByDepartmentAndPosition(
            @PathVariable("dapartid") int dapartid,
            @PathVariable("positionid") int positionid,
            HttpServletResponse response, HttpServletRequest request) {
        //条件查询管理员
        List<User> users = userService.selectUserByCondition2(dapartid, positionid);
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setBeanList(users);
        //查询所有部门
        List<Departments> departments = departmentsDao.selectAllDepartment();
        userPageBean.setDepartments(departments);
        //查询所有的职位
        List<Position> positions = userService.selectAllPosition();
        userPageBean.setPositions(positions);

        return userPageBean;
    }

    @PostMapping("/usersbycondition/{pnum}/{positionid}/{username}/{pageIndex}")
    public PageBean<User> selectUserByDepartmentAndPositionAndUsername(
            @PathVariable("pnum") int pnum,
            @PathVariable("positionid") int positionid,
            @PathVariable("username") String username,
            @PathVariable("pageIndex") int pageIndex,
            HttpServletResponse response, HttpServletRequest request) {

        System.out.println(pageIndex + "=====================================");


        if (username.equals("null")) {
            username = null;
        }
        //条件查询管理员
        List<User> users = userService.selectUserByCondition3(pnum, positionid, username, pageIndex, 5);
        System.out.println(users + "------------------------------------------------------------------------------------------");
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setBeanList(users);
        //查询所有部门
        List<Departments> departments = departmentsDao.selectAllDepartment();
        userPageBean.setDepartments(departments);
        //查询所有的职位
        List<Position> positions = userService.selectPositionByCondition(pnum);
        userPageBean.setPositions(positions);
        userPageBean.setTotalRecord(userService.selectAllUserCount(pnum, positionid));
        return userPageBean;
    }


    @PostMapping("/selectPositionBypnum/{setdapartid}")
    public List<Position> selectPositionBypnum(
            @PathVariable("setdapartid") int setdapartid,
            HttpServletResponse response, HttpServletRequest request) {
        //查询所有的职位
        List<Position> positions = userService.selectPositionByCondition(setdapartid);
        return positions;
    }

    @PostMapping("/selectPositionByPname/{setdapartid}")
    public List<Position> selectPositionByPname(
            @PathVariable("setdapartid") String setdapartid,
            HttpServletResponse response, HttpServletRequest request) {
        //查询所有的职位
        List<Position> positions = userService.selectPositionByCondition2(setdapartid);
        return positions;
    }


    @PostMapping("/updateUser/{id}/{setpositionid}/{pnum}/{positionid}/{username}/{pageIndex}")
    public PageBean<User> updateUser(
            @PathVariable("id") int id,
            @PathVariable("setpositionid") int setpositionid,
            @PathVariable("pnum") int pnum,
            @PathVariable("positionid") int positionid,
            @PathVariable("username") String username,
            @PathVariable("pageIndex") int pageIndex,
            HttpServletResponse response, HttpServletRequest request) {
        userService.updateUser(id, setpositionid);


        if (username.equals("null")) {
            username = null;
        }

        //条件查询管理员
        List<User> users = userService.selectUserByCondition3(pnum, positionid, username, pageIndex, 5);
        System.out.println(users + "------------------------------------------------------------------------------------------");
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setBeanList(users);
        //查询所有部门
        List<Departments> departments = departmentsDao.selectAllDepartment();
        userPageBean.setDepartments(departments);
        //查询所有的职位
        List<Position> positions = userService.selectPositionByCondition(pnum);
        userPageBean.setPositions(positions);
        userPageBean.setTotalRecord(userService.selectAllUserCount(pnum, positionid));


        return userPageBean;
        //return "操作成功";
    }

    @PostMapping("/deleteUser/{id}/{pnum}/{positionid}/{username}/{pageIndex}")
    public PageBean<User> deleteUser(
            @PathVariable("id") int id,

            @PathVariable("pnum") int pnum,
            @PathVariable("positionid") int positionid,
            @PathVariable("username") String username,
            @PathVariable("pageIndex") int pageIndex,
            HttpServletResponse response, HttpServletRequest request) {
        //删除user
        userService.deleteUser(id);


        if (username.equals("null")) {
            username = null;
        }

        //条件查询管理员
        List<User> users = userService.selectUserByCondition3(pnum, positionid, username, pageIndex, 5);
        System.out.println(users + "------------------------------------------------------------------------------------------");
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setBeanList(users);
        //查询所有部门
        List<Departments> departments = departmentsDao.selectAllDepartment();
        userPageBean.setDepartments(departments);
        //查询所有的职位
        List<Position> positions = userService.selectPositionByCondition(pnum);
        userPageBean.setPositions(positions);
        userPageBean.setTotalRecord(userService.selectAllUserCount(pnum, positionid));


        return userPageBean;
        //return "操作成功";
    }

    @PostMapping("/resetpassword/{id}")
    public void resetpassword(
            @PathVariable("id") int id){
        userService.resetpassword(id,"$2a$10$pr/QvAJek4i6RIp8pwF.5.xp4woe6b3IvyqqbrNQpbdkY1fKXjPq.");

    }

    @PostMapping("/updatepassword/{id}")
    public void updatepassword(
            @PathVariable("id") int id){

    }

    @PostMapping("/selectUserByEstatus")
    public List<User02> selectUserByEstatus(){
        List<User02> user02s = userDao.selectUserByEstatus();
        return user02s;
    }

    @PostMapping("/insertUserByEstatus")
    public void insertUserByEstatus(
            @RequestBody User02 user
    ){
        //添加
        userService.insertUser(user);
    }

}